<?php

class CommentService extends Service_Base 
{
	
	/**
	 * Getting comment select from criteria
	 * 
	 * @param CommentCriteria $criteria
	 * @return Db_Select|null
	 */
	public function getComments(CommentCriteria $criteria)
	{
		$peer = CommentPeer::getInstance();
		$select = $peer->select();
		// add substance type id limitation
		$substanceType = $criteria->getSubstanceType();
		$substanceTypeRow = CommentSubstanceTypePeer::getInstance()->findByTable($substanceType)->current();
		if (!$substanceTypeRow) {
			return null;
		}
		$select->where(CommentPeer::SUBSTANCE_TYPE_ID, $substanceTypeRow->getId());
		// add substance id limitation
		$substanceId = $criteria->getSubstanceId();
		if (!$substanceId) {
			return null;
		}
		$select->where(CommentPeer::SUBSTANCE_ID, $substanceId);
		
		// adding limit & order 
		$select = parent::addLimitConstraint($select, $criteria);
		$select = parent::addOrderConstraint($select, $criteria);
		return $select;
	}
}